﻿using Common.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CToolsWin.Network
{
    public partial class Requester : Form
    {
        private int AddHeaderTimes = 0;
        private int AddParamTimes = 0;
        public Requester()
        {
            InitializeComponent();
            tabControl1.SelectedIndex = 1;
            txtUrl.Focus();
        }

        private void btnAddHeader_Click(object sender, EventArgs e)
        {
            AddTextBox(ref AddHeaderTimes, this.tabPage1, "txtHeader", 197);
        }

        private void btnAddParam_Click(object sender, EventArgs e)
        {
            AddTextBox(ref AddParamTimes, this.tabPage2, "txtParam", 35);
        }

        private void AddTextBox(ref int times, TabPage tabPage, string txtKey, int startLocation)
        {
            times++;
            TextBox txtBoxKey = new TextBox();
            TextBox txtBoxValue = new TextBox();

            tabPage.Controls.Add(txtBoxKey);
            tabPage.Controls.Add(txtBoxValue);

            txtBoxKey.Location = new System.Drawing.Point(150, startLocation + times * 27);
            txtBoxKey.Name = txtKey + times + "Key";
            txtBoxKey.Size = new System.Drawing.Size(370, 21);

            txtBoxValue.Location = new System.Drawing.Point(9, startLocation + times * 27);
            txtBoxValue.Name = txtKey + times + "Value";
            txtBoxValue.Size = new System.Drawing.Size(135, 21);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            int timeOut = 0;
            Int32.TryParse(txtHeaderTimeout.Text, out timeOut);
            HttpHeaders headers = new HttpHeaders();
            headers.timeout = timeOut;
            headers.contentType = txtHeaderContentType.Text;
            headers.host = txtHeaderHost.Text;
            headers.accept = txtHeaderAccept.Text;
            headers.connection = txtHeaderConnection.Text;
            headers.referer = txtHeaderReferer.Text;
            headers.userAgent = txtHeaderUserAgent.Text;

            Dictionary<string, string> dicParams = new Dictionary<string, string>();
            Dictionary<string, string> dicHeaders = new Dictionary<string, string>();

            foreach (Control ctl in this.Controls)
            {
                if (ctl.Name.StartsWith("txtHeader") && ctl.Name.EndsWith("Key"))
                {
                    if (string.IsNullOrWhiteSpace(ctl.Text) == false)
                    {
                        foreach (Control ctl2 in this.Controls)
                        {
                            if (ctl2.Name.Equals(ctl.Name.Replace("Key", "Value")))
                            {
                                dicHeaders.Add(ctl.Text, ctl2.Text);
                                continue;
                            }
                        }
                    }
                }
                else if (ctl.Name.StartsWith("txtParam") && ctl.Name.EndsWith("Key"))
                {
                    if (string.IsNullOrWhiteSpace(ctl.Text) == false)
                    {
                        foreach (Control ctl2 in this.Controls)
                        {
                            if (ctl2.Name.Equals(ctl.Name.Replace("Key", "Value")))
                            {
                                dicParams.Add(ctl.Text, ctl2.Text);
                            }
                        }
                    }
                }
            }

            if (cbbMethod.SelectedIndex == 0)
                txtResult.Text = HttpHelper.Get(txtUrl.Text, Encoding.UTF8, dicParams, headers,dicHeaders);
            else
                txtResult.Text = HttpHelper.Post(txtUrl.Text, Encoding.UTF8, dicParams, headers, dicHeaders);
        }
    }
}
